package com.carl.zlds.dao.sqlprovider;

import com.carl.zlds.pojo.dto.ArticleSearchDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;

import java.util.HashMap;

/**
 * @Author: carl
 * @Date: 2020/6/24 2:29 PM
 */
public class ArticleSqlProvider {
    public String getArticleList(HashMap<String, Object> param) {
        ArticleSearchDTO dto = (ArticleSearchDTO) param.get("dto");
        String limitSql = (String) param.get("limitSql");
        StringBuilder sql = new StringBuilder();
        if (limitSql.isEmpty()) {
            sql.append(" select count(a.id) ");
        } else {
            sql.append(" select a.* ");
        }
        sql.append(" from article a ");
        sql.append(" where a.weight >= 0 ");
        if (Strings.isNotEmpty(dto.getTitle())) {
            sql.append(" and a.title like concat('%', #{dto.title},'%') ");
        }
        sql.append(" order by a.create_time desc ");

        System.out.println(sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY));
        return sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY);
    }
}
